﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Fluent;
using Techstar.Controls;
using SmartMD.DataModel;
using System;

namespace SmartMD.Manager
{
    public class BaseWindow : DocumentContent
    {
        private bool _isChanged;
        private bool _isSave = true;
        private string _title;

        private static IList<BaseWindow> _instances;

        private IList<RibbonTabItem> _ribbonPages;

        public BaseWindow()
        {
            Instances.Add(this);
        }

        public static IList<BaseWindow> Instances
        {
            get
            {
                if (_instances == null)
                    _instances = new List<BaseWindow>();
                return _instances;
            }
        }
        public virtual void DeleteEntity(Object entity)
        {
        }
        public static BaseWindow ActiveWindow
        {
            get
            {
                if (MainWindow.Instance.DocumentManager.ActiveDocument != null && MainWindow.Instance.DocumentManager.ActiveDocument is BaseWindow)
                    return (BaseWindow)MainWindow.Instance.DocumentManager.ActiveDocument;
                return null;
            }
        }

        /// <summary>
        /// 注册工具栏
        /// </summary>
        /// <param name="toolbar"></param>
        protected void RegisterToolbar(ToolBar toolbar)
        {
            //((Panel)toolbar.Parent).Children.Remove(toolbar);
            //this.Toolbars.Add(new WpfToolbar(this, toolbar));
        }

        protected void RegisterToolbar(RibbonTabItem toolbar)
        {
            ((Panel)toolbar.Parent).Children.Remove(toolbar);
            this.Toolbars.Add(toolbar);
            toolbar.Tag = this;
        }

        /// <summary>
        /// 该窗口的工具页
        /// </summary>
        public IList<RibbonTabItem> Toolbars
        {
            get { return _ribbonPages ?? (_ribbonPages = new List<RibbonTabItem>()); }
        }

        protected virtual void RefreshToolBar(bool active = true)
        {
            foreach (var toolbar in this.Toolbars)
            {
                toolbar.Visibility = active ? Visibility.Visible : Visibility.Collapsed;
            }
            if (Toolbars.Count > 0 && (MainWindow.Instance != null) && active)
                MainWindow.Instance.tabToolbar.SelectedTabItem = Toolbars[0];

        }

        protected override void OnIsActiveDocumentChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnIsActiveDocumentChanged(e);
            //this.Focus();
            var active = ((bool)e.NewValue);
            MainWindow.Instance.tabToolbar.SelectedTabItem = MainWindow.Instance.tabHome;
            foreach (var toolbar in Toolbars)
            {
                if ((MainWindow.Instance != null) && !MainWindow.Instance.tabToolbar.Tabs.Contains(toolbar))
                {
                    MainWindow.Instance.tabToolbar.Tabs.Add(toolbar);
                }
            }

            RefreshToolBar(active);
            if (active)
            {
                OnWindowActivated();
            }
        }
        /**
         * 功能 ： 激活窗口
         * 
         * */

        protected virtual void OnWindowActivated()
        {

        }

        public override bool Close()
        {
            try
            {
                return base.Close();

            }
            catch
            {
                return base.Close();
            }
        }

        protected override void OnClosed()
        {
            base.OnClosed();
            Instances.Remove(this);
            var i = 0;
            while (i < MainWindow.Instance.tabToolbar.Tabs.Count)
            {
                if ((MainWindow.Instance.tabToolbar.Tabs[i]).Tag == this)
                {
                    MainWindow.Instance.tabToolbar.Tabs.RemoveAt(i);
                    break;
                }
                else
                {
                    i++;
                }
            }
        }

        //protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        //{
        //    if (((FormClosingEventArgs)(e)).CloseReason == CloseReason.MdiFormClosing)
        //        return;
        //    if (this.IsChanged)
        //    {
        //var list = new List<BaseForm> { this };
        //var result = SaveDialogForm.ShowEx(list);
        //switch (result)
        //{
        //    case System.Windows.Forms.DialogResult.Yes:
        //        {
        //            var isSave = this.Save();
        //            if (!isSave)
        //            {
        //                this.BringToFront();
        //                e.Cancel = true;
        //                return;
        //            }
        //        }
        //        break;
        //    case System.Windows.Forms.DialogResult.Cancel:
        //        e.Cancel = true;
        //        return;
        //}
        //    }
        //}

        /// <summary>
        /// 是否已被修改，用于判断当前是否需要保存时使用
        /// </summary>
        public bool IsChanged
        {
            get
            {
                return _isChanged;
            }
            set
            {
                _isChanged = value;
                OnTitleChange();
            }
        }

        /// <summary>
        /// 该模块保存方法是否可用，如果为true，则会把保存按钮加到当前窗口的工具栏上
        /// </summary>
        public virtual bool IsSave
        {
            get
            {
                return _isSave;
            }
            set
            {
                _isSave = value;
            }
        }

        /// <summary>
        /// 加载当前要编辑的对象
        /// </summary>
        /// <param name="param"></param>
        public virtual void Load(object param)
        {

        }

        /// <summary>
        /// 新建对象
        /// </summary>
        /// <param name="param">一般情况是传要创建的对象所属父,也可以传其它内容</param>
        public virtual void New(object param)
        {

        }

        /// <summary>
        /// 执行保存方法
        /// 保存成功返回true否则返回false
        /// </summary>
        public virtual bool Save()
        {
            return true;
        }

        /// <summary>
        /// 更改窗口标题
        /// </summary>
        private void OnTitleChange()
        {
            base.Title = this.IsChanged ? _title + "*" : _title;
        }

        /// <summary>
        /// 通知标题发生变化
        /// </summary>
        /// <param name="title"></param>
        private void OnTitleChange(string title)
        {
            this._title = title;
            OnTitleChange();
        }

        /// <summary>
        /// 窗口的标题名称
        /// </summary>
        public new string Title
        {
            get
            {
                return _title;
            }
            set
            {
                OnTitleChange(value);
            }
        }

        /// <summary>
        /// 更新工具栏上的按钮状态
        /// </summary>
        public virtual void RefreshRibbonPageState()
        {

        }

        /// <summary>
        /// 当前窗口正在编辑的实体
        /// </summary>
        public EntityBase Entity
        {
            get;
            set;
        }
    }

    public class WpfToolbar
    {
        private RibbonTabItem _toolbar;
        private BaseWindow _owner;

        public WpfToolbar(BaseWindow owner, RibbonTabItem bar)
        {
            _owner = owner;
            _toolbar = bar;
        }

        public RibbonTabItem ToolBar
        {
            get
            {
                return _toolbar;
            }
        }

        public BaseWindow Owner
        {
            get
            {
                return _owner;
            }
        }
    }
}
